/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package kimo.persistence.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import kimo.persistence.entities.ProductType;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import kimo.persistence.controllers.exceptions.IllegalOrphanException;
import kimo.persistence.controllers.exceptions.NonexistentEntityException;
import kimo.persistence.entities.ProductCategory;

/**
 *
 * @author a381u296
 */
public class ProductCategoryJpaController implements Serializable {

    public ProductCategoryJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ProductCategory productCategory) {
        if (productCategory.getProductTypeList() == null) {
            productCategory.setProductTypeList(new ArrayList<ProductType>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<ProductType> attachedProductTypeList = new ArrayList<ProductType>();
            for (ProductType productTypeListProductTypeToAttach : productCategory.getProductTypeList()) {
                productTypeListProductTypeToAttach = em.getReference(productTypeListProductTypeToAttach.getClass(), productTypeListProductTypeToAttach.getProductTypeId());
                attachedProductTypeList.add(productTypeListProductTypeToAttach);
            }
            productCategory.setProductTypeList(attachedProductTypeList);
            em.persist(productCategory);
            for (ProductType productTypeListProductType : productCategory.getProductTypeList()) {
                ProductCategory oldProductTypeCategoryIdOfProductTypeListProductType = productTypeListProductType.getProductTypeCategoryId();
                productTypeListProductType.setProductTypeCategoryId(productCategory);
                productTypeListProductType = em.merge(productTypeListProductType);
                if (oldProductTypeCategoryIdOfProductTypeListProductType != null) {
                    oldProductTypeCategoryIdOfProductTypeListProductType.getProductTypeList().remove(productTypeListProductType);
                    oldProductTypeCategoryIdOfProductTypeListProductType = em.merge(oldProductTypeCategoryIdOfProductTypeListProductType);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ProductCategory productCategory) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ProductCategory persistentProductCategory = em.find(ProductCategory.class, productCategory.getProductCategoryId());
            List<ProductType> productTypeListOld = persistentProductCategory.getProductTypeList();
            List<ProductType> productTypeListNew = productCategory.getProductTypeList();
            List<String> illegalOrphanMessages = null;
            for (ProductType productTypeListOldProductType : productTypeListOld) {
                if (!productTypeListNew.contains(productTypeListOldProductType)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain ProductType " + productTypeListOldProductType + " since its productTypeCategoryId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<ProductType> attachedProductTypeListNew = new ArrayList<ProductType>();
            for (ProductType productTypeListNewProductTypeToAttach : productTypeListNew) {
                productTypeListNewProductTypeToAttach = em.getReference(productTypeListNewProductTypeToAttach.getClass(), productTypeListNewProductTypeToAttach.getProductTypeId());
                attachedProductTypeListNew.add(productTypeListNewProductTypeToAttach);
            }
            productTypeListNew = attachedProductTypeListNew;
            productCategory.setProductTypeList(productTypeListNew);
            productCategory = em.merge(productCategory);
            for (ProductType productTypeListNewProductType : productTypeListNew) {
                if (!productTypeListOld.contains(productTypeListNewProductType)) {
                    ProductCategory oldProductTypeCategoryIdOfProductTypeListNewProductType = productTypeListNewProductType.getProductTypeCategoryId();
                    productTypeListNewProductType.setProductTypeCategoryId(productCategory);
                    productTypeListNewProductType = em.merge(productTypeListNewProductType);
                    if (oldProductTypeCategoryIdOfProductTypeListNewProductType != null && !oldProductTypeCategoryIdOfProductTypeListNewProductType.equals(productCategory)) {
                        oldProductTypeCategoryIdOfProductTypeListNewProductType.getProductTypeList().remove(productTypeListNewProductType);
                        oldProductTypeCategoryIdOfProductTypeListNewProductType = em.merge(oldProductTypeCategoryIdOfProductTypeListNewProductType);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = productCategory.getProductCategoryId();
                if (findProductCategory(id) == null) {
                    throw new NonexistentEntityException("The productCategory with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ProductCategory productCategory;
            try {
                productCategory = em.getReference(ProductCategory.class, id);
                productCategory.getProductCategoryId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The productCategory with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<ProductType> productTypeListOrphanCheck = productCategory.getProductTypeList();
            for (ProductType productTypeListOrphanCheckProductType : productTypeListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This ProductCategory (" + productCategory + ") cannot be destroyed since the ProductType " + productTypeListOrphanCheckProductType + " in its productTypeList field has a non-nullable productTypeCategoryId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(productCategory);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ProductCategory> findProductCategoryEntities() {
        return findProductCategoryEntities(true, -1, -1);
    }

    public List<ProductCategory> findProductCategoryEntities(int maxResults, int firstResult) {
        return findProductCategoryEntities(false, maxResults, firstResult);
    }

    private List<ProductCategory> findProductCategoryEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ProductCategory.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ProductCategory findProductCategory(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ProductCategory.class, id);
        } finally {
            em.close();
        }
    }

    public int getProductCategoryCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ProductCategory> rt = cq.from(ProductCategory.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
